<template>
	<view class="app-container">
		<view class="search-box">
			<uni-search-bar v-model="keywords" radius="100" placeholder="请输入店名搜索" clearButton="none"
				cancelButton="none" />
		</view>
		<view class="search-history" v-if="!keywords">
			<view class="title-box">
				<text class="title">最近搜索</text>
				<uni-icons type="trash" size="40rpx" @click="handleClearHistory"></uni-icons>
			</view>
			<view class="search-history-list">
				<text class="no-search-history" v-if="!historyList.length">暂无搜索记录</text>
				<text class="search-history-item" v-for="(item, index) in historyList" :key="index"
					@click="handleHistory(item)">{{ item }}</text>
			</view>
		</view>
		<view class="search-result" v-else>
			<view class="search-result-item" @click="handleSearch('/pages/bar/list')">
				<image class="search-icon" src="/static/images/internet-bar-icon.png" mode="widthFix"></image>
				<view class="content">
					<text class="keywords u-line-1">{{ keywords }}</text>
					<text class="dot">·</text>
					<text class="type-text">网吧</text>
				</view>
				<uni-icons type="right" size="32rpx" color="#ACAEB9"></uni-icons>
			</view>
			<view class="search-result-item" @click="handleSearch('/pages/hotel/list')">
				<image class="search-icon" src="/static/images/hotel-icon.png" mode="widthFix"></image>
				<view class="content">
					<text class="keywords u-line-1">{{ keywords }}</text>
					<text class="dot">·</text>
					<text class="type-text">电竞酒店</text>
				</view>
				<uni-icons type="right" size="32rpx" color="#ACAEB9"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive, toRefs
	} from "vue";
	import {
		onLoad
	} from '@dcloudio/uni-app';
	
	const state = reactive({
		storageKey: "history-search",
		keywords: "",
		historyList: []
	})
	
	const { storageKey, keywords, historyList } = toRefs(state)
	
	onLoad(() => {
		historyList.value = JSON.parse(uni.getStorageSync(storageKey.value) || "[]")
	})
	
	// 点击搜索
	function handleSearch(page) {
		historyList.value.unshift(keywords.value);
		// 去重
		historyList.value = [...new Set(historyList.value)];
		if (historyList.value.length > 15) {
			historyList.value.pop();
		}
		uni.setStorageSync(storageKey.value, JSON.stringify(historyList.value));
		uni.navigateTo({
			url: `${page}?keywords=${keywords.value}`
		})
	}
	
	// 清空历史搜索
	function handleClearHistory() {
		uni.showModal({
			title: '温馨提示',
			content: `是否确认清空最近搜索？`,
			showCancel: true,
			cancelText: '取消',
			confirmText: '确认',
			success: async (res) => {
				if (res.confirm) {
					historyList.value = [];
					uni.setStorageSync(storageKey.value, JSON.stringify(historyList.value));
				}
			}
		});
	}
	
	// 点击历史记录
	function handleHistory(item) {
		keywords.value = item;
	}
</script>

<style scoped lang="scss">
	.app-container {
		background-color: #FFFFFF;
		
		.search-box {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100rpx;
			margin: 0 20rpx;
			
			uni-search-bar {
				width: 100%;
			}
		}

		.search-history {
			padding: 0 20rpx;

			.title-box {
				display: flex;
				justify-content: space-between;

				.title {
					font-size: 32rpx;
					color: var(--default-font-color);
				}
			}

			.search-history-list {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				gap: 20rpx;
				font-size: 24rpx;

				.no-search-history {
					width: 100%;
					text-align: center;
					line-height: 100rpx;
					color: var(--gray-font-color);
				}

				.search-history-item {
					background-color: #F4F4F6;
					padding: 10rpx 30rpx;
					border-radius: 40rpx;
					color: var(--default-font-color);
				}
			}
		}

		.search-result {
			.search-result-item {
				margin: 0 20rpx;
				height: 112rpx;
				display: flex;
				align-items: center;
				border-bottom: 1px solid rgba(18, 24, 54, 0.08);
				font-size: 28rpx;
				color: var(--default-font-color);
				font-weight: 400;

				.search-icon {
					width: 50rpx;
					margin-right: 20rpx;
					display: block;
				}

				.content {
					flex: 1;
					display: flex;
					align-items: center;

					.keywords {
						color: var(--primary-color);
					}

					.dot {
						font-weight: bold;
						margin: 0 10rpx;
					}

					.type-text {
						min-width: 120rpx;
					}
				}
			}
		}
	}
</style>